package com.generator.sysManage.sectionManage.service.impl;

import com.generator.sysManage.sectionManage.dao.IDMMap;
import com.generator.sysManage.sectionManage.pojo.DMDept;
import com.generator.sysManage.sectionManage.service.IDMService;
import com.generator.sysManage.userManage.pojo.*;
import com.generator.util.PageModel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * DMService
 * @author WJW
 * @Date .....
 * @version ......
 */
@Service
public class DMService implements IDMService{

    @Autowired
    private IDMMap idmMap;

    public IDMMap getIdmMap() {
        return idmMap;
    }
    public void setIdmMap(IDMMap idmMap) {
        this.idmMap = idmMap;
    }



    /**
     * 根据条件查询所有部门集合
     * @param dmDept 查询条件
     * @return 部门集合
     */
    public List<DMDept> getDMDeptListByDMDept(DMDept dmDept){
        return idmMap.getDMDeptListByDMDept(dmDept);
    }


    /**
     * 根据条件查询所有部门集合进行分页
     * @param dmDept 查询条件
     * @return pageModel
     */
    public PageModel<DMDept> getDMDeptListByFY(DMDept dmDept, PageModel<DMDept> pageModel){
        pageModel.setPageNum(pageModel.getPageNum()<=0?1:pageModel.getPageNum());
        pageModel.setPageSize(pageModel.getPageSize()<=0?3:pageModel.getPageSize());
        PageHelper.startPage(pageModel.getPageNum(),pageModel.getPageSize());

        List<DMDept> userList = idmMap.getDMDeptListByDMDept(dmDept);


        PageInfo<DMDept> pageInfo = new PageInfo<DMDept>(userList);

        pageModel.setData(new ArrayList<DMDept>(userList));
        pageModel.setTotalPage(pageInfo.getPages());
        pageModel.setTotalRows(pageInfo.getTotal());

        return pageModel;
    }

    /**
     * 根据条件查询所有省
     * @param province 查询条件
     * @return 省集合
     */
    public List<Province> getProvinceList(Province province){
        return idmMap.getProvinceList(province);
    }


    /**
     * 根据条件查询所有市
     * @param city 查询条件
     * @return 市集合
     */
    public List<City> getCityList(City city){
        return idmMap.getCityList(city);
    }


    /**
     * 搜索部门集合进行分页
     * @param dmDept 搜索条件
     * @return pageModel
     */
    public PageModel<DMDept> getDMDeptListByFY1(DMDept dmDept, PageModel<DMDept> pageModel){
        pageModel.setPageNum(pageModel.getPageNum()<=0?1:pageModel.getPageNum());
        pageModel.setPageSize(pageModel.getPageSize()<=0?3:pageModel.getPageSize());
        PageHelper.startPage(pageModel.getPageNum(),pageModel.getPageSize());

        List<DMDept> userList = idmMap.getDMDeptListByDMDept(dmDept);


        PageInfo<DMDept> pageInfo = new PageInfo<DMDept>(userList);

        pageModel.setData(new ArrayList<DMDept>(userList));
        pageModel.setTotalPage(pageInfo.getPages());
        pageModel.setTotalRows(pageInfo.getTotal());

        return pageModel;
    }


    /**
     * 添加部门
     * @param dmDept 添加的部门信息
     * @return 执行条数
     */
    public Integer addDMDept(DMDept dmDept){
        return idmMap.addDMDept(dmDept);
    }


    /**
     *进入修改页面
     * @param dmDept 存放dept_id
     * @return 页面路径
     */
    public String updateView(DMDept dmDept, Model model){
        List<DMDept> dmDeptList = idmMap.getDMDeptListByDMDept(dmDept);
        model.addAttribute("updateDept",dmDeptList.get(0));
        List<Province> provinces = idmMap.getProvinceList(new Province());
        model.addAttribute("provinceList1",provinces);
        List<City> cityList = idmMap.getCityList(new City());
        model.addAttribute("cityList1",cityList);
        return "sys/dept/deptUpdate";
    }


    /**
     *进入搜索后的修改页面
     * @param dmDept 存放dept_id
     * @return 页面路径
     */
    public String updateView1(DMDept dmDept, Model model){
        List<DMDept> dmDeptList = idmMap.getDMDeptListByDMDept(dmDept);
        model.addAttribute("updateDept",dmDeptList.get(0));
        List<Province> provinces = idmMap.getProvinceList(new Province());
        model.addAttribute("provinceList1",provinces);
        List<City> cityList = idmMap.getCityList(new City());
        model.addAttribute("cityList1",cityList);
        return "sys/dept/deptUpdate1";
    }



    /**
     *修改部门
     * @param dmDept 修改的部门信息
     * @return 执行条数
     */
    public Integer updateDMDept(DMDept dmDept){
        return idmMap.updateDMDept(dmDept);
    }





    /**
     *修改部门状态
     * @param dmDept 修改的部门信息
     * @return 路径
     */
    public String  updateState(DMDept dmDept, Model model){
        String dept_id1 = dmDept.getDept_id();
        String[] split = dept_id1.split(",");

        String dept_id=split[0];
        DMDept dept=new DMDept();
        dept.setDept_id(dept_id);

        int pageNum=Integer.parseInt(split[1]);
        PageModel<DMDept> pageModel=new PageModel<>();
        pageModel.setPageNum(pageNum);

        if(split[2].equals("0")){
            dept.setDept_state("1");
        }else {
            dept.setDept_state("0");
        }

        Integer add = idmMap.updateState(dept);
        if(add==1){
            DMDept dept1=new DMDept();
            PageModel<DMDept> deptList = getDMDeptListByFY(dept1,pageModel);
            model.addAttribute("deptList",deptList);
            return "sys/dept/deptList";
        }
        return "forward:/deptManage/deptList";
    }


    /**
     *修改部门状态
     * @param dmDept 修改的部门信息
     * @return 搜索后路径
     */
    public String  updateState1(DMDept dmDept, Model model,DMDept dmDept1){
        String dept_id1 = dmDept.getDept_id();
        String[] split = dept_id1.split(",");

        String dept_id=split[0];
        DMDept dept=new DMDept();
        dept.setDept_id(dept_id);

        int pageNum=Integer.parseInt(split[1]);
        PageModel<DMDept> pageModel=new PageModel<>();
        pageModel.setPageNum(pageNum);

        if(split[2].equals("0")){
            dept.setDept_state("1");
        }else {
            dept.setDept_state("0");
        }

        Integer add = idmMap.updateState(dept);
        if(add==1){
            PageModel<DMDept> deptList = getDMDeptListByFY1(dmDept1,pageModel);
            model.addAttribute("deptList",deptList);
            return "sys/dept/deptList1";
        }
        return "forward:/deptManage/deptList";
    }

}
